#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: 蒋艰坚
@file: image_information.py
@time: 2021/8/18 22:41
@desc: 
"""
import os
import re

import exifread


def find_date_image(pic_path):
    date = ''
    with open(pic_path, 'rb') as f:
        tags = exifread.process_file(f)
        for tag, value in tags.items():
            if re.match('.*Date.*', tag):
                date = str(value)
    f.close()
    return date


def sort_image_list(file_path):
    # 函数名：sort_image_list（图片列表排序）
    # 参数：图片文件夹路径 建议：文件夹中仅含有图片信息
    # 返回值：排序后图像名列表及各图像对应的时间字典
    dir_list = os.listdir(file_path)
    if not dir_list:
        return
    else:
        dir_list = sorted(
            dir_list, key=lambda x: find_date_image(file_path + x))
    dir_dict = {}
    for item in dir_list:
        date = find_date_image(file_path + item).split(" ")[0]
        dir_dict[item] = date
    return dir_list, dir_dict


if __name__ == '__main__':
    dir_list, dir_dict = sort_image_list('D:/time_test/')
    print(dir_list)
    print(dir_dict)
